/*
 * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
 *
 * Author:     fanpengcheng <fanpengcheng_cm@deepin.com>
 *
 * Maintainer: fanpengcheng <fanpengcheng_cm@deepin.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "mainwindow.h"
#include "switchbutton.h"
#include "brightnesswindow.h"
#include "firewalldetailpage/firewalldetailpage.h"
#include "csv/csvwidget.h"

#include "timercount/timercountlauncherwindow.h"
#include "graph-move/graphmovewindow.h"
#include "movebycursor/movebycursorwindow.h"
#include "qsspage/qsspage.h"
#include "callbackpage/callbackpage.h"
#include "prepostcondition/prepostconditionpage.h"
#include "ctableview/ctableviewwindow.h"
#include "cchart/cchartwidget.h"

#include <DTitlebar>
#include <DListView>
#include <QStandardItemModel>
#include <DStandardItem>
#include <DWidgetUtil>
#include <DComboBox>

#include <QApplication>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QResizeEvent>
#include <QVBoxLayout>
#include <QDebug>
#include <QSize>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QPainterPath>
#include <QPainter>
#include <QPushButton>
#include <QMessageBox>
#include <qmath.h>
#include <QPainter>
#include <QCompleter>

DWIDGET_USE_NAMESPACE

#define MAX_BTN_NUM (48)
#define MAX_CNT_ROW (6)

MainWindow::MainWindow(QWidget *parent)
    : DMainWindow(parent)
    , m_icon(nullptr)
{
    //主窗口重新设置
    setMinimumSize(900, 500);
    titlebar()->setIcon(QIcon(":/image/cat.png"));
    Dtk::Widget::moveToCenter(this);

    login();
    initMember();
    initWidget();

    //布局管理初始化
    QWidget *centerWindow; //QWidget类主窗口，以此窗口进行布局
    centerWindow = new QWidget;
    setCentralWidget(centerWindow);
    //centerWindow->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QHBoxLayout *turnWindowLayout = new QHBoxLayout;
    centerWindow->setLayout(turnWindowLayout);

    //使用DListView对窗口进行管理
    DListView *leftLstview = new DListView(this);
    leftLstview->setItemSize(QSize(100, 48));

    //初始化左边选项列表模型
    m_leftLstMod = new QStandardItemModel(this);

    //初始化右边布局管理
    QWidget *rightWindow = new QWidget(centerWindow);
    m_rightLayout = new QHBoxLayout;
    rightWindow->setLayout(m_rightLayout);
    //rightWindow->setStyleSheet("background-color:red");

    //分配左右布局比例
    turnWindowLayout->addWidget(leftLstview, 2);
    turnWindowLayout->addWidget(rightWindow, 7);

    //////////////////////////////////////////////
    /// 基本按钮页面
    QHBoxLayout *layout = new QHBoxLayout;

    QPushButton *button1 = new QPushButton("test1");
    QPushButton *button2 = new QPushButton("test2");
    QPushButton *button3 = new QPushButton("test3");

    layout->addWidget(button1, 10);
    layout->addWidget(button2, 5);
    layout->addWidget(button3, 1);

    m_baseBtnView = new QWidget(this);
    m_baseBtnView->hide();
    m_baseBtnView->setLayout(layout);

    /////////////////////////
    //在此处使用addWindow添加窗口
    CChartWidget *chartWidget = new CChartWidget(this);
    addWindow("chartWidget", QIcon(), chartWidget);

    // ctableview
    CTableViewWindow *cTableViewWindow = new CTableViewWindow(this);
    addWindow("cTableViewWindow", QIcon(), cTableViewWindow);

    // prepostcondition
    PrePostConditionPage *prePostConditionPage = new PrePostConditionPage(this);
    addWindow("prePostConditionPage", QIcon(), prePostConditionPage);

    // callback page
    CallbackPage *callbackPage = new CallbackPage(this);
    addWindow("callback_page", QIcon(), callbackPage);

    // qss
    QSSPage *qssPage = new QSSPage(this);
    addWindow("qss page", QIcon(), qssPage);

    addWindow("开关阵列", QIcon(":/image/cat.svg"), m_switchView);
    addWindow("基本按钮", QIcon(":/image/cat.png"), m_baseBtnView);

    // combox
    QWidget *comboBoxWindow = new QWidget(this);
    QVBoxLayout *comboBoxLayout = new QVBoxLayout(comboBoxWindow);
    comboBoxWindow->setLayout(comboBoxLayout);

    DComboBox *comboBox = new DComboBox(comboBoxWindow);
    comboBox->setEditable(true);

    QCompleter *pCompleter = new QCompleter(comboBox->model(), this);
    comboBox->setCompleter(pCompleter);

    comboBox->addItems({"q", "dasfa", "uioinj"});

    comboBoxLayout->addWidget(comboBox);

    addWindow("333", QIcon(), comboBoxWindow);

    BrightnessWindow *brightWindow = new BrightnessWindow(this);
    addWindow("亮度", QIcon(), brightWindow);

    FirewallDetailPage *firewallPage = new FirewallDetailPage(this);
    addWindow("firewall", QIcon(), firewallPage);

    CSVWidget *csvWidget = new CSVWidget(this);
    addWindow("csv", QIcon(), csvWidget);

    TimerCountLauncherWindow *tcWindow = new TimerCountLauncherWindow(this);
    addWindow("timer-count", QIcon(), tcWindow);

    GraphMoveWindow *gmWindow = new GraphMoveWindow(this);
    addWindow("图形移动", {}, gmWindow);

    MoveByCursorWindow *mbcWindow = new MoveByCursorWindow(this);
    addWindow("随鼠标移动窗口", {}, mbcWindow);

    //////////////////////
    //设置左边选项列表属性
    leftLstview->setModel(m_leftLstMod);
    leftLstview->setIconSize(QSize(50, 50));
    //leftLstview->setCurrentIndex(m_leftLstMod->index(0,0));
    //////////////////////

    connect(leftLstview, &DListView::clicked, this, &MainWindow::turnWindow);
}

MainWindow::~MainWindow()
{
    delete m_icon;
}

void MainWindow::paintEvent(QPaintEvent *)
{
    double k = 10;

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QColor my_color(237, 162, 255, 255);
    QBrush my_brush(my_color);
    painter.setPen(Qt::red);
    painter.translate(this->width() / 2, this->height() / 2);
    QPainterPath polygonPath;
    polygonPath.setFillRule(Qt::WindingFill);

    float x = 16 * k * sin(0.0) * sin(0.0) * sin(0.0);
    float y = 13 * k * cos(0.0) - 5 * k * cos(0.0) - 2 * k * cos(0.0) - cos(0.0);
    polygonPath.moveTo(x, -y);

    for (double t = 0.01; t < 100; t += 0.05) {
        x = 16 * k * sin(k * t) * sin(k * t) * sin(k * t);
        y = 13 * k * cos(k * t) - 5 * k * cos(2 * k * t) - 2 * k * cos(3 * k * t) - cos(4 * k * t);
        polygonPath.lineTo(x, -y);
    }
    painter.drawPath(polygonPath);
    painter.drawLine(80, 100, 650, 500);
    painter.setPen(Qt::red);
    painter.drawRect(10, 10, 100, 400);
    painter.setPen(QPen(Qt::green, 5));
    painter.setBrush(Qt::blue);
    painter.drawEllipse(50, 150, 400, 200);

    if (!m_isRegister)
        return;

    if (m_phoneNumber.isEmpty()) {
        // messagebox : please input phone number
        return;
    }
}

void MainWindow::initMember()
{
    m_icon = new DIconButton(this);
    m_icon->setIcon(QIcon(":/image/sort_select.png"));
    connect(m_icon, &DIconButton::clicked, this, &MainWindow::slot_iconBtnClicked);
    //DIconButton does not update its size at initialization time, it needs ResizeEvent to trigger once
    resize(size().width() + 1, size().height() + 1);

    for (int i = 0; i < MAX_BTN_NUM; ++i) {
        SwitchButton *btn = new SwitchButton();
        btn->setRate(3);
        btn->setChecked(false);
        btn->setShapeType(e_Rect);
        m_btnVec.push_back(btn);
    }
}

void MainWindow::initWidget()
{
    m_switchView = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(m_switchView);
    mainLayout->setSpacing(3);
    mainLayout->setMargin(0);
    //add btn
    mainLayout->addWidget(m_icon);

    QGridLayout *bodyLayout = new QGridLayout();
    QVector<SwitchButton *>::const_iterator it = m_btnVec.begin();
    for (int i = 0; it != m_btnVec.end(); ++it, ++i) {
        bodyLayout->addWidget(*it, i / MAX_CNT_ROW, i % MAX_CNT_ROW);
    }
    mainLayout->addLayout(bodyLayout);

    //setCentralWidget(content);
    setAccessibleName("test-mainwindow");
    setMouseTracking(true);
    setAcceptDrops(true);
}

void MainWindow::slot_iconBtnClicked()
{
    static bool check = true;
    QVector<SwitchButton *>::const_iterator it = m_btnVec.begin();
    for (; it != m_btnVec.end(); ++it) {
        SwitchButton *btn = static_cast<SwitchButton *>(*it);
        btn->setChecked(check);
        btn->setShapeType(check ? e_Ellipse : e_Rect);
    }
    check = !check;
}

void MainWindow::login()
{
    if (!m_isRegister)
        return;

    if (m_phoneNumber.isEmpty()) {
        // messagebox : please input phone number
        return;
    }
}

void MainWindow::turnWindow(const QModelIndex &index)
{
    int row = index.row();
    qDebug() << row;
    if (false == m_rightWidgetLstOnShow.isEmpty()) {
        QWidget *widgetTmp = m_rightWidgetLstOnShow.last();
        m_rightWidgetLstOnShow.pop_back();
        widgetTmp->hide();
    }

    //检索点击了哪个按钮
    for (int i = 0; m_rightWidgetLst.size() > i; i++) {
        if (i == row) {
            m_rightWidgetLstOnShow.push_back(m_rightWidgetLst.at(row));
        }
    }

    m_rightWidgetLstOnShow.last()->show(); //打开显示队列中窗口的显示开关
}

void MainWindow::closeEvent(QCloseEvent *event)
{
}

void MainWindow::addWindow(QString itemName, QIcon icon, QWidget *window)
{
    //添加左侧选项名称和图标
    QStandardItem *item = new QStandardItem(itemName);
    if (icon.isNull()) {
        item->setIcon(QIcon(":/image/cat.png")); //设置默认图标
    } else {
        item->setIcon(icon);
    }
    m_leftLstMod->appendRow(item);

    //向右侧布局中添加新窗口
    window->hide(); //初始为隐藏
    m_rightLayout->addWidget(window);

    //向右侧窗口队列添加新窗口
    m_rightWidgetLst.push_back(window);
}
